from flask import Blueprint, make_response, request, redirect,render_template
import uuid

from APP import LoginForm

cookie_bp = Blueprint(
    'cookie_bp',
    __name__,
    url_prefix= '/cookie',
    template_folder='/APP/templates'
    )


@cookie_bp.before_request
def dog_doors():
    if request.path=='/cookie/setcookie':
        form = LoginForm(request.form)
        userid = form.user_id.data
        password = form.password.data
        if not (userid and password):
            print('password或userid不能为空')
            return redirect('/')
    elif request.path=='/':
        pass
    else:
        username = request.cookies.get('UserID')
        if not username:
            print('failed')
            return render_template('is_spider.html')
        else:
            print('success')

@cookie_bp.route('/setcookie',methods = ['GET','POST'])
def set_cookie():
    response = make_response('cookie设置成功')
    response.set_cookie('UserID',str(uuid.uuid1()))
    return response

@cookie_bp.route('/getcookie')
def get_cookie():
    userid = request.cookies.get('UserID')
    if userid:
        return '您当前的UserID为:%s'%userid
    else:
        return render_template('is_spider.html')




